local GameDB = import(".GameDB")

function GameDB:getClassSkill(skillId)
    local res = self:getCachedValue("class_skill",skillId)
    return res
end

--[[--
-- 查询技能名称
]]
function GameDB:getSkillName(tid)
    if tid and tonumber(tid)>0 then
        local skillName = self:getClassSkill(tid).name
        return skillName
    else
        return ""
    end
end

--查询下一个等级的技能
function GameDB:queryNextLevelSkill(skillId)
    local curSkillConf = self:getClassSkill(skillId)

    local nextSkillId = tonumber(skillId)+1
	local nextSkillConf = self:getClassSkill(nextSkillId)

     -- 下一个连续id技能的等级 不是和本技能等级连续
     -- 说明 下一个连续id的技能 其实不是 "queryNextLevelSkill"
    if curSkillConf and nextSkillConf and tonumber( nextSkillConf.skillLv ) - tonumber( curSkillConf.skillLv ) > 1 then
        return
    end
    if nextSkillConf then
        if nextSkillConf.collection then
            nextSkillConf.collectionName=self:getCollectionName(nextSkillConf.collection)
        end
        if nextSkillConf.preSkill then
            nextSkillConf.preSkillName=self:getSkillName(nextSkillConf.preSkill)
        end
    end
	return nextSkillConf
end


-- 通过技能组取一组技能
function GameDB:querySkillsByTeam( team )
    local res = self:getCachedValues("class_skill",
        function(v)
            return tonumber(v.team) == tonumber( team )
        end,
        function(a,b)
            return tonumber( a.ID ) < tonumber( b.ID )
        end)
    return res
end

-- 根据class获取所有的talent配置，并缓存起来
-- teamConfDict 以skillTeam为key缓存
-- specConfsDict 以专精分组缓存
GameDB._talentClassConfDict = {}
function GameDB:getTalentClassConf(class)
    local confDict = self._talentClassConfDict[class]
    if not confDict then
        local confs = self:getCachedValues("talent",
            function(v)
                return tonumber(v.class) == class
            end)

        local teamConfDict = {}
        local specConfsDict = {}
        local specId
        for i,conf in ipairs(confs) do
            teamConfDict[tonumber(conf.skillteam)] = conf
            specId = tonumber(conf.followSkill)
            specConfsDict[specId] = specConfsDict[specId] or {}
            table.insert(specConfsDict[specId], conf)
        end

        -- 按位置排序，但感觉没有必要
        local function sortFunc(a, b)
            return tonumber(a.seat) < tonumber(b.seat)
        end
        for i,confs in ipairs(specConfsDict) do
            table.sort(confs, sortFunc)
        end

        confDict = {}
        confDict.teamConfDict = teamConfDict
        confDict.specConfsDict = specConfsDict
    end
    return confDict
end

-- 获得某class的所有talent（按专精分组）
function GameDB:getTalentConfsByClass(class)
    local confDict = self:getTalentClassConf(class)
    return confDict.specConfsDict
end

-- 获得某class的某专精的所有talent
function GameDB:getTalentConfsByClassAndSpecId(class, specId)
    local confDict = self:getTalentClassConf(class)
    return confDict.specConfsDict[specId] or {}
end

-- 获得某skillTeam的talent配置
function GameDB:getTalentConfByTeam(class, team)
    local confDict = self:getTalentClassConf(class)
    return confDict.teamConfDict[team]
end

-- 根据职业Id返回专精Id列表
function GameDB:getSpec_typeConfigByClass(class)
	local spec_typeConfig = self:getCachedValues("spec_type",
        function(v)
            return tonumber(v.class) == tonumber(class)
        end, 
        function(a, b)
            return tonumber(a.sort) < tonumber(b.sort)
        end)
	return spec_typeConfig
end

function GameDB:getSpec_typeConfig(id)
	--local sql = string.format("SELECT * FROM spec_type WHERE ID=%d",id)
	local spec_typeConfig = self:getCachedValue("spec_type",id)
	return spec_typeConfig
end

function GameDB:getSkillIdByLvAndTeam( lv, team )
    local skillConfig = self:getCachedValue("class_skill",
        function(v)
            return tonumber( v.skillLv ) == tonumber( lv )
                and tonumber( v.team ) == tonumber( team )
        end)
    return skillConfig
end

function GameDB:getFirstSkillConfByTeam(team)
    return self:getSkillIdByLvAndTeam(1, team)
end

function GameDB:getSpecSkillChoose(id)
    return self:getCachedValue("specSkill_choose",id)
end


----------------------------->>>>>>>>>> 以下：处理符文
-- 优先品质降序
-- 然后ID升序
local function orderFunc( a, b )
    if a.quality and b.quality then
        return tonumber( a.quality ) > tonumber( b.quality )
    else
        return tonumber( a.ID ) < tonumber( b.ID )
    end
end

-- 取 rune 表中 特定 class、equipPlace 字段的项合集
-- class 这里是专精id
function GameDB:queryRuneByClassAndEquipPlace( classTemp, equipPlaceTemp )
    return self:getCachedValues("rune",
        function( v )
            return tonumber( v.class ) == tonumber( classTemp ) and
                    tonumber( v.equipPlace ) == tonumber( equipPlaceTemp )
        end,
        orderFunc )
end

-- 取 rune 表中 特定 class、team 字段的项合集
function GameDB:queryRuneByClassAndTeam( classTemp, teamTemp )
    return self:getCachedValues("rune",
        function( v )
            return tonumber( v.class ) == tonumber( classTemp ) and
                    tonumber( v.team ) == tonumber( teamTemp )
        end,
        orderFunc )
end

-- 取 rune 表中 特定 team 字段的项合集
function GameDB:queryRunesByTeam( teamTemp )
    return self:getCachedValues("rune",
        function( v )
            return tonumber( v.team ) == tonumber( teamTemp )
        end,
        orderFunc )
end

-- 取 rune 表中 特定 class、equipPlace 字段的项合集
function GameDB:queryRuneById( runeId )
    return self:getCachedValue("rune",runeId)
end
-----------------------------<<<<<<<<<< 以上

return GameDB
